package com.company.recruit.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.company.recruit.common.R;
import com.company.recruit.entity.*;
import com.company.recruit.service.RecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @author wanghengfeng
 * @email 2428284819@qq.com
 * @date 2021-12-02 17:05:21
 */
@CrossOrigin
@RestController
@RequestMapping("/recruit")
public class RecruitController {
    @Autowired
    RecruitService recruitService;

    @Autowired
    private ThreadPoolExecutor executor;

    /**
     *  获取所有原数据接口
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        List<RecruitEntity> list = recruitService.list();

        return R.ok().put("list", list);
    }


    /**
     *  获取所有  -- 每个地区的平均工资（最高和最低）
     */
    @RequestMapping("/list/area/avg/wage")
    public R getAreaAvgWageList() {
        List<AreaAvgWage> data =  recruitService.getAll();
        return R.ok().put("data", data);
    }



    /**
     *  获取所有 -- 学历占比
     */
    @RequestMapping("/list/education/analyse")
    public R getEducationAnalyseList() {
        List<EducationAnalyse> data =  recruitService.getAllEducationAnalyse();
        return R.ok().put("data", data);
    }


    /**
     *  获取所有 -- 每个地区的招聘人数
     */
    @RequestMapping("/list/recruit/num")
    public R getRecruitNumList() {
        List<RecruitNum> data =  recruitService.getAllRecruitNum();
        return R.ok().put("data", data);
    }



    /**
     *  获取所有 -- 各个工作经验类型 -- 对应人数 -- 分区
     */
    @RequestMapping("/list/experience/type/analyse")
    public R getExperienceTypeAnalyseList() {
        List<ExperienceTypeAnalyse> data = recruitService.getAllExperienceTypeAnalyseByList();
        return R.ok().put("data", data);
    }


    /**
     *  获取所有 -- 各个福利类型 -- 对应数量
     */
    @RequestMapping("/list/welfare/analyse")
    public R getWelfareAnalyseList() {
        // 查询初始数据
        List<WelfareAnalyse> data =  recruitService.getWelfareByAnalyseByList();
        return R.ok().put("data",data);
    }

    // 上面是直接获取数据



    // --------------------------------------------------------------  下面是计算

    /**
     *  生成 -- 所有方法统一接口
     */
    @RequestMapping("/get/all/data")
    public R getAllData() throws ExecutionException, InterruptedException {

        // 第一个异步任务  --   生成 -- 各个工作经验类型 -- 对应人数 -- 分区
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            // 查询
            List<ExperienceTypeAnalyse> data = recruitService.getAllExperienceTypeAnalyse();
            // 保存
            recruitService.saveExperienceTypeAnalyse(data);

            // 重新查询所有
            List<ExperienceTypeAnalyse> dataClean = recruitService.getAllExperienceTypeAnalyseByClean();
        });

        // 第二个异步任务 --
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            // 查询初始数据
            List<WelfareAnalyse> data = recruitService.getWelfareByAnalyse();
        });

        // 第三个异步任务 -=-
        CompletableFuture<Void> voidCompletableFuture3 = CompletableFuture.runAsync(() -> {
            recruitService.getRecruitNumByService();
        });


        // 第四个异步任务 -=-
        CompletableFuture<Void> voidCompletableFuture4 = CompletableFuture.runAsync(() -> {
            // 获取统计信息
            List<EducationAnalyse> data = recruitService.getEducationAnalyse();

            // 保存数据
            data.stream().forEach((educationAnalyse) -> {
                recruitService.saveEducationAnalyse(educationAnalyse);
            });
        });


        // 第五个异步任务 - --
        CompletableFuture<Void> voidCompletableFuture5 = CompletableFuture.runAsync(() -> {
            recruitService.getAreaAvgWageByService();
        });



        CompletableFuture.allOf( voidCompletableFuture1,voidCompletableFuture2,voidCompletableFuture3,voidCompletableFuture4,voidCompletableFuture5 ).get();



        return R.ok();
    }



    /**
     *  生成 -- 各个工作经验类型 -- 对应人数 -- 分区
     */
    @RequestMapping("/get/experience/type/analyse")
    public R getExperienceTypeAnalyse() {
        // 查询
        List<ExperienceTypeAnalyse> data = recruitService.getAllExperienceTypeAnalyse();
        // 保存
        recruitService.saveExperienceTypeAnalyse(data);

        // 重新查询所有
        List<ExperienceTypeAnalyse> dataClean = recruitService.getAllExperienceTypeAnalyseByClean();

        return R.ok().put("data", dataClean);
    }



    /**
     *  生成 -- 各个福利类型 -- 对应数量
     */
    @RequestMapping("/get/welfare/analyse")
    public R getWelfareAnalyse() {
        // 查询初始数据
        List<WelfareAnalyse> data =  recruitService.getWelfareByAnalyse();
        return R.ok().put("data",data);
    }




    /**
     *  生成  -- 地区招聘人数分类
     */
    @RequestMapping("/get/recruit/num")
    public R getRecruitNum() {
        recruitService.getRecruitNumByService();
        return R.ok();
    }




    /**
     *  生成 -- 学历分布分类
     */
    @RequestMapping("/get/education/analyse")
    public R getEducationAnalyse() {

        // 获取统计信息
        List<EducationAnalyse> data = recruitService.getEducationAnalyse();

        // 保存数据
        data.stream().forEach((educationAnalyse) -> {
            recruitService.saveEducationAnalyse(educationAnalyse);
        });

        return R.ok().put("data", data);
    }



    /**
     *  生成  -- 计算每个地区的平均工资（最高和最低）
     */
    @RequestMapping("/get/area/avg/wage")
    public R getAreaAvgWage() {
        recruitService.getAreaAvgWageByService();
        return R.ok();
    }



    // ---------------------------------------------



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("storage:recruit:info")
    public R info(@PathVariable("id") Integer id) {
        RecruitEntity recruit = recruitService.getById(id);

        return R.ok().put("recruit", recruit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("storage:recruit:save")
    public R save(@RequestBody RecruitEntity recruit) {
        recruitService.save(recruit);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("storage:recruit:update")
    public R update(@RequestBody RecruitEntity recruit) {
        recruitService.updateById(recruit);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("storage:recruit:delete")
    public R delete(@RequestBody Integer[] ids) {
        recruitService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    public void deliveryEntity(RecruitEntity recruitEntity) {
        if (!StringUtils.isEmpty(recruitEntity)) {
            recruitService.save(recruitEntity);
        }
    }
}
